package com.hrt.manage.product.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.StaleObjectStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.ProductDetails;
import com.hrt.bean.ProductInfor;
import com.hrt.manage.product.dao.ProInforDao;
import com.hrt.manage.product.dao.ProductDetailsDao;
import com.hrt.manage.product.service.ProDetailsService;
import com.hrt.manage.product.service.ProInforService;

@Service
@Transactional
public class ProductDetailsServiceImpl implements ProDetailsService {

	@Autowired
	private ProductDetailsDao dao;

	@Autowired
	private ProInforService proInforSvr;

	@Autowired
	private ProInforDao proInforDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly=false)
	public void saveDetail(ProductDetails detail) {

		try {
			dao.saveEntity(detail);
			proInforSvr.updateTotal(detail.getProductInfor());
		
		} 
		catch(StaleObjectStateException ex){
			try {
				ProductDetails lastDetail=dao.getById(detail.getId());
			
				BeanUtils.copyProperties(lastDetail, detail);
				dao.updateEntity(lastDetail);
				proInforSvr.updateTotal(lastDetail.getProductInfor());
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public List<ProductDetails> queryByPage(int pageIndex, int pageSize) {

		return dao.queryByPage(pageIndex, pageSize);
	}

	@Override
	@Transactional
	public void deleteById(Long[] proIds) {
		try {
			ProductInfor product = null;
			List<ProductDetails> list = dao.getByIds(proIds);
			for (ProductDetails detail : list) {
				detail.setIsdelete(new Integer(1));
				dao.updateEntity(detail);
				if (product == null) {
					product = detail.getProductInfor();
				}

			}
			proInforSvr.updateTotal(product);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public ProductDetails getDetailById(Long deId) {

		return dao.getById(deId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly=false)
	public boolean udateDetail(ProductDetails details) throws StaleObjectStateException, IllegalAccessException, InvocationTargetException {

		try {
			dao.updateEntity(details);
			proInforSvr.updateTotal(details.getProductInfor());
			return true;
		} 
		catch(StaleObjectStateException ex){
			
				ProductDetails lastDetail=dao.getById(details.getId());
				
				lastDetail.setColor(details.getColor());
				lastDetail.setCreateId(details.getCreateId());
				lastDetail.setCreator(details.getCreator());
				lastDetail.setCreatTime(details.getCreatTime());
				lastDetail.setDescription(details.getDescription());
				lastDetail.setEditor(details.getEditor());
				lastDetail.setEditorId(details.getEditorId());
				lastDetail.setEditTime(details.getEditTime());
				lastDetail.setEnterPrice(details.getEnterPrice());
				lastDetail.setHeight(details.getHeight());
				lastDetail.setIsdelete(details.getIsdelete());
				lastDetail.setIsdemo(details.getIsdemo());
				lastDetail.setLongness(details.getLongness());
				lastDetail.setMinTotal(details.getMinTotal());
				lastDetail.setPic(details.getPic());
				lastDetail.setProCode(details.getProCode());
				lastDetail.setProductInfor(details.getProductInfor());
				lastDetail.setProName(details.getProName());
				lastDetail.setRuleCode(details.getRuleCode());
				lastDetail.setRuleName(details.getRuleName());
				lastDetail.setSalePrice(details.getSalePrice());
				lastDetail.setTotal(details.getTotal());
				lastDetail.setUseful(details.getUseful());
				lastDetail.setWidth(details.getWidth());
				dao.updateEntity(lastDetail);
				proInforSvr.updateTotal(lastDetail.getProductInfor());
				
				return true;
		
			
			
		}
		

	}

	@Override
	public Long totalCount(Long proId) {

		return dao.totalCount(proId);
	}

	@Override
	public List<ProductDetails> queryByProId(Long proId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
		List<ProductDetails> list = new ArrayList<ProductDetails>();
		Map<String, Object> con = new HashMap<String, Object>();
		con.put("field", "proId");
		con.put("value", proId);

		Map<String, Object> delcon = new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list = dao.queryPageByCondition(conditions, pageIndex, pageSize);

		return list;
	}

	@Override
	public List<ProductDetails> queryByProId(Long proId) {
		List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
		List<ProductDetails> list = new ArrayList<ProductDetails>();
		Map<String, Object> con = new HashMap<String, Object>();
		con.put("field", "proId");
		con.put("value", proId);

		Map<String, Object> delcon = new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", new Integer(0));
		conditions.add(con);
		conditions.add(delcon);
		list = dao.queryByCondition(conditions);

		return list;
	}

	@Override
	public List<ProductDetails> queryProducts(String condtionStr) {

		List<ProductDetails> list = new ArrayList<ProductDetails>();

		list = dao.queryProducts(condtionStr);
		return list;
	}

	/**
	 * 查询所有
	 * */
	@Override
	public List<ProductDetails> searchDelAll() {

		return this.dao.findDelAll();
	}

	@Override
	public ProductDetails getDetailByProIdAndRuleId(Long proId, Long ruleId) {
		return dao.getDetailByProIdAndRuleId(proId, ruleId);
	}

	/** 通过产品id查找产品规格 */
	@Override
	public List<ProductDetails> getProDetailByProId(Long proId) {
		return this.dao.getProDetailByProId(proId);
	}

	/**
	 * 按id列表查询实体
	 * */
	@Override
	public ProductDetails getById(Long id) {
		return this.dao.getById(id);
	}

	@Override
	public Long getProInforDetailTotals(Long ruleId) {
		// TODO Auto-generated method stub
		Long total = dao.getProInforDetailTotals(ruleId);
		if (total == null) {
			total = 0l;
		}
		return total;
	}

}
